﻿using Core;
using Main;
using System.Collections.Generic;

public class RoleInfoWidget : FGUIWidget<UI_RoleInfo>
{
    private ListAdapter<UI_ItemSlot> listAdapter;
    private List<Info> list = new List<Info>();
    public Unit Owner { get; private set; }

    protected override void OnInitialize(params object[] args)
    {
        listAdapter = new ListAdapter<UI_ItemSlot>(m_root.m_equipList);
        m_root.m_attributeList.itemRenderer = (index, gObject) =>
        {
            var compoennt = gObject as UI_Attribute;
            compoennt.m_title.text = list[index].title;
            compoennt.m_value.text = list[index].value;
        };

        listAdapter.SetItemRenderer((index, component) =>
        {
            var inventory = Owner.GetComponent<InventoryComponent>();
            var slot = inventory.equipment[index];
            var config = World.GetComponent<ConfigComponent>().GetConfig<ItemConfig>(slot.itemId);
            component.m_icon.url = config?.icon;
            component.m_count.visible = false;
        });

        listAdapter.SetClick((index, component) =>
        {
            var inventory = Owner.GetComponent<InventoryComponent>();
            var slot = inventory.equipment[index];
            var curSlot = inventory.CurSlot;

            if (inventory.CurSlot != null && inventory.CurSlot.container == inventory.equipment)
            {
                inventory.ChangeCurrentItemSlot(null);
                if(curSlot == slot)
                {
                    return;
                }
            }

            curSlot = inventory.CurSlot;
            if (curSlot == null)
            {
                inventory.ChangeCurrentItemSlot(slot);
                return;
            }
            if(inventory.CurSlot == slot)
            {
                inventory.ChangeCurrentItemSlot(null);
                return;
            }

            if (!curSlot.IsEmpty)
            {
                var config = World.GetComponent<ConfigComponent>().GetConfig<ItemConfig>(curSlot.itemId);
                if (config != null && config.itemType == (int)ItemType.Equipment && config.itemSubType - 10 == index)
                {
                    inventory.Swap(curSlot, slot);
                    inventory.ChangeCurrentItemSlot(null);
                }
            }
            else
            {
                inventory.ChangeCurrentItemSlot(slot);
            }
        });

        listAdapter.SetTouchBegin((index, component, _) =>
        {
            var inventory = Owner.GetComponent<InventoryComponent>();
            var slot = inventory.equipment[index];
            World.GetComponent<EventComponent>().Publish(new TouchBeginItemSlot { slot = slot, component = component });
        });

        listAdapter.SetTouchEnd((index, component, _) =>
        {
            var inventory = Owner.GetComponent<InventoryComponent>();
            var slot = inventory.equipment[index];
            World.GetComponent<EventComponent>().Publish(new TouchEndItemSlot { slot = slot, component = component });
        });

        listAdapter.SetLongPress((index, component) => { });
    }

    public void SelectSlot(int index, bool isSelect)
    {
        var selectComponent = listAdapter.GetChild(index);
        selectComponent.m_select.visible = isSelect;
    }

    public void Bind(Unit unit)
    {
        this.Owner = unit;
        UpdateInfo();

        listAdapter.UpdateCount(Owner.GetComponent<InventoryComponent>().equipment.Count);
    }

    public void UpdateInfo()
    {
        list.Clear();

        var langComponent = World.GetComponent<LangComponent>();
        var component = Owner.GetComponent<NumericComponent>();
        var loader = World.GetComponent<ConfigComponent>().GetLoader<NumbericConfigLoader>();

        addAttribute(NumericType.Hp);
        addAttribute(NumericType.Attack);
        addAttribute(NumericType.AttackDistance);

        void addAttribute(int attribute)
        {
            var config = loader.Get(attribute);
            if (config == null)
                return;

            var value = component[attribute];
            string valueStr = !config.isPercentum ? $"{value}" : $"{value}%";
            list.Add(new Info { title = langComponent.Get(config.name), value = valueStr });
        }


        m_root.m_attributeList.numItems = list.Count;
        listAdapter.UpdateCount(Owner.GetComponent<InventoryComponent>().equipment.Count);
    }

    struct Info
    {
        public string title;
        public string value;
    }
}
